﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using JigLibX.Collision;
using JigLibX.Physics;
using JigLibX.Geometry;
using JigLibX.Math;
using JigLibX.Utils;

namespace Innovator
{
	public class HeightMapObject : PhysicsObject
	{
		public HeightMapInfo info;
		public HeightMapObject(HeightMapInfo heightMapInfo, Vector2 shift)
		{
			Setup(heightMapInfo, shift);
			InitializeComponent(Engine.DefaultScreen);
		}
		public HeightMapObject(HeightMapInfo heightMapInfo, Vector2 shift, GameScreen Parent)
		{
			Setup(heightMapInfo, shift);
			InitializeComponent(Parent);
		}
		void Setup(HeightMapInfo heightMapInfo, Vector2 shift)
		{
			Body = new Body();
			CollisionSkin = new CollisionSkin(null);
			info = heightMapInfo;
			Array2D field = new Array2D(heightMapInfo.Heights.GetUpperBound(0), heightMapInfo.Heights.GetUpperBound(1));
			for (int x = 0; x < heightMapInfo.Heights.GetUpperBound(0); ++x)
			{
				for (int z = 0; z < heightMapInfo.Heights.GetUpperBound(1); ++z)
				{
					field.SetAt(x,z,heightMapInfo.Heights[x,z]);
				}
			}
			Body.MoveTo(new Vector3(shift.X,0,shift.Y),Matrix.Identity);
			CollisionSkin.AddPrimitive(new Heightmap(field,shift.X,shift.Y,1,1),
				(int)MaterialTable.MaterialID.UserDefined,
				new MaterialProperties(0.7f,0.7f,0.6f));
			PhysicsSystem.CurrentPhysicsSystem.CollisionSystem.AddCollisionSkin(CollisionSkin);
		}
	}
}